import React, {PureComponent} from 'react';
import {FinderModal, FinderSelect} from 'jh-lib';
import {Button, Card} from "antd";
import ComponentType from "../../Config/ComponentType";
import '../../../global.less'
import {pick} from "lodash-es";
import Promise from "lodash-es/_Promise";
import finderConfJson from "../../FinderSelect/finderConf";

const x = 3;
const y = 2;
const z = 1;
const gData = [];

const generateData = (_level, _preKey, _tns) => {
    const preKey = _preKey || '0';
    const tns = _tns || gData;

    const children = [];
    for (let i = 0; i < x; i++) {
        const code = `${preKey}-${i}`;
        tns.push({title: code, key: code, name: code, code: code});
        if (i < y) {
            children.push(code);
        }
    }
    if (_level < 0) {
        return tns;
    }
    const level = _level - 1;
    children.forEach((code, index) => {
        tns[index].children = [];
        return generateData(level, code, tns[index].children);
    });
};
generateData(z);

const cols = [
    {
        remark: '应用',
        name: 'app',
        floatNum: '',
        sum: false,
        reg: '',
        expression: '',
        format: '',
        align: '',
        colWidth: 100,
        componentType: ComponentType.FINDER,
        finderCode: 'USER',
        appCode: 'USER'
    },
    {
        remark: '顺序',
        name: 'sort',
        floatNum: '0',
        sum: false,
        reg: '',
        expression: '',
        format: '',
        align: '',
        colWidth: 100,
        componentType: ComponentType.NUMBER_INPUT,
    }
]

export default class BaseTest extends PureComponent {

    state = {
        detailLoading: false
    };
    startIndex = 0;

    fetchFinder(ajaxData) {
        console.log('fetchFinder ajaxData', ajaxData);

        return new Promise((resolve) => {

            const arr = [];

            for (let i = ajaxData.startIndex; i < ajaxData.startIndex + ajaxData.maxResults; i++) {
                this.startIndex++;
                let id = i + 1;
                arr.push(
                    {id: id, code: `code${id}`, name: `name${id}`, spellCode: `spellCode${id}`},
                )
            }

            setTimeout(() => {
                resolve({
                    status: 200,
                    data: {
                        results: arr,
                        resultCount: 100,
                        maxResults: 20,
                    }
                });
            }, 1000);
        });
    }

    selectedRows = [];

    render() {
        return (
            <>
                <Card title={'FinderModal'}>
                    <div>
                        <code>
                            {JSON.stringify(this.selectedRows)}
                        </code>
                    </div>
                    <Button onClick={() => {
                        this.setState({isShowModal: true});
                    }}>open</Button>
                    <FinderModal
                        appins={this}
                        finderCode={'FINDER_ACCOUNTINGITEM'}
                        appCode={''}
                        fetchConf={(ajaxData) => {
                            console.log('fetchConf ajaxData', ajaxData);
                            return new Promise((resolve) => {
                                setTimeout(() => {
                                    resolve(finderConfJson);
                                }, 1000);
                            });
                        }}
                        // defaultSelectedRows={this.selectedRows}
                        multi={true}
                        title={'选择（标题）'}
                        visible={this.state.isShowModal}
                        onOk={(selectedRows) => {
                            console.log('selectedRows', selectedRows);
                            this.selectedRows = selectedRows;
                            this.setState({isShowModal: false});
                        }}
                        onCancel={() => {
                            this.setState({isShowModal: false});
                        }}
                        onRef={(ref) => {
                            this.finderModalIns = ref;
                        }}
                    />
                </Card>
            </>
        )
    }
}